package com.fzx.fzwsupplysystem.util;

import android.os.Handler;

/**
 * 项目：AndroidUtilsDemo
 * 创建人：FangZhiXin
 * 时间：2019/11/18 14:55
 * 描述：
 */
public class CountTimes {

    private int mMillisInFuture;//开始计时秒数
    private int mInterval;//间隔秒数
    private int mEndCount = Integer.MAX_VALUE;//截至计时秒数

    private boolean isCancel = true;
    private boolean isCounting = true;
    private int reTime;

    private OnCountTimeListener mListener;

    public interface OnCountTimeListener {
        //启动定时就调用
        void onStart(int time);

        //第一次计时开始算，每次计时都调用
        void onTick(int time);

        void onCancel();

        void onFinish(int time);
    }

    public void startTimeDown(int countTime, int countdownInterval, OnCountTimeListener listener) {
        isCancel = false;
        isCounting = true;
        mListener = listener;
        mMillisInFuture = countTime;
        mInterval = countdownInterval;
        reTime = countTime;
        handler.removeCallbacks(runnable);
        handler.removeCallbacks(countRunnable);
        handler.postDelayed(runnable, Math.abs(mInterval) * 1000);
        if (mListener != null) {
            mListener.onStart(mMillisInFuture);
        }
    }

    public void startTimeCount(int startTime, int endTime, int interval, OnCountTimeListener listener) {
        isCancel = false;
        isCounting = true;
        mListener = listener;
        mInterval = Math.abs(interval);
        mMillisInFuture = startTime;
        mEndCount = endTime;
        reTime = startTime;
        handler.removeCallbacks(runnable);
        handler.removeCallbacks(countRunnable);
        handler.postDelayed(countRunnable, Math.abs(mInterval) * 1000);
        if (mListener != null) {
            mListener.onStart(mMillisInFuture);
        }
    }

    public boolean isCancel() {
        return isCancel;
    }

    public void resetTime() {
        mMillisInFuture = reTime;
        if (mListener != null) {
            mListener.onStart(mMillisInFuture);
        }
    }

    public void restart() {
        if (isCounting) {
            isCancel = false;
            handler.removeCallbacks(runnable);
            handler.removeCallbacks(countRunnable);
            handler.postDelayed(countRunnable, Math.abs(mInterval) * 1000);
            if (mListener != null) {
                mListener.onStart(mMillisInFuture);
            }
        }
    }

    public void cancel() {
        isCancel = true;
        handler.removeCallbacks(runnable);
        handler.removeCallbacks(countRunnable);
        if (mListener != null) {
            mListener.onCancel();
        }
    }

    public void release() {
        isCancel = true;
        isCounting = false;
        handler.removeCallbacks(runnable);
        handler.removeCallbacks(countRunnable);
        mListener = null;
    }

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (isCancel) {
                return;
            }
            if (mInterval == 0) {
                return;
            }
            mMillisInFuture = mMillisInFuture - mInterval;
            int interval = Math.abs(mInterval);
            int delay;
            if (mInterval > 0) {
                if (mMillisInFuture <= 0) {
                    if (mListener != null) {
                        mListener.onFinish(0);
                    }
                } else {
                    if (mMillisInFuture < interval) {
                        delay = mMillisInFuture;
                    } else {
                        delay = interval;
                    }
                    handler.postDelayed(this, delay * 1000);
                    if (mListener != null) {
                        mListener.onTick(mMillisInFuture);
                    }
                }
            } else {
                handler.postDelayed(this, interval * 1000);
                if (mListener != null) {
                    mListener.onTick(mMillisInFuture);
                }
            }

        }
    };

    private Runnable countRunnable = new Runnable() {
        @Override
        public void run() {
            if (isCancel) {
                return;
            }
            if (mInterval == 0 || mMillisInFuture == mEndCount) {
                return;
            }
            int delay;
            if (mMillisInFuture < mEndCount) {//正计时
                mMillisInFuture = mMillisInFuture + mInterval;
                if (mMillisInFuture >= mEndCount) {
                    if (mListener != null) {
                        mListener.onFinish(mEndCount);
                    }
                } else {
                    if (mEndCount - mMillisInFuture < mInterval) {
                        delay = mEndCount - mMillisInFuture;
                    } else {
                        delay = mInterval;
                    }
                    handler.postDelayed(this, delay * 1000);
                    if (mListener != null) {
                        mListener.onTick(mMillisInFuture);
                    }
                }
            } else {//倒计时
                mMillisInFuture = mMillisInFuture - mInterval;
                if (mMillisInFuture <= mEndCount) {
                    if (mListener != null) {
                        mListener.onFinish(mEndCount);
                    }
                } else {
                    if (mMillisInFuture - mEndCount < mInterval) {
                        delay = mMillisInFuture - mEndCount;
                    } else {
                        delay = mInterval;
                    }
                    handler.postDelayed(this, delay * 1000);
                    if (mListener != null) {
                        mListener.onTick(mMillisInFuture);
                    }
                }
            }


        }
    };


}
